<?php
	//todo - 	do a more flexible image replacement that can cater for different image sizes, either
	//			by using gd to create an image, or gd to nicely resize and image or have functions to
	//			declare multiple images for different size/case requirements
	//
	//			setBackupImage is basic and doesn't check if it exists, asbolute or relative, etc!
	//
	//current-	load default replacement image (and set default image link to class)

	//class dedicated to image checking and replacement
	//-------------------------------
	//yes this class could be static, but is a fully fledged class
	//to remain consistent with other CostBiter classes
	//
	// NOTE: if a relative image link is supplied, there isn't any checking if the server is http or https, assumes http.

	//-------------------------------------
	//test code for class
	//-------------------------------------
//	$ImageChecker = new ImageChecker();
	
//	error_reporting(E_ALL); 
//	ini_set("display_errors", 1);
	
//	$ImageChecker->enableDebugMessages();
//	$ImageChecker->showImg("http://dev.costbiter.co.uk/web3g/img/costbiter/5052271000000.jpg", 146, 106, "", "", "");	//valid absolute image
//	$ImageChecker->showImg("http://dev.costbiter.co.uk/web3g/img/costbiter/5052sd271000000.jpg", 146, 106, "", "", "");	//file doesn't exist
//	echo $ImageChecker->showImg("http://dev.costbiter.co.uk/web3g/inc/ImageChecker.inc.php", 146, 106, "", "", "");	//not an image
//	echo $ImageChecker->showImg("web3g/img/costbiter/5052271000000.jpg", 146, 106, "", "", "");	//valid relative image

//	$ImageChecker->showImg("http://www.pearshapedcode.com/gallery/cleanups/plough.jpg", 146, 106, "", "", "");	//external image
//	$ImageChecker->showImg("http://www.google.co.uk/intl/en_uk/images/logo.gif", 146, 106, "", "", "");	//external image
	//-------------------------------------

	
	class ImageChecker {
		//class vars
		private $singlebackupimage;			//renewed by calling the setbackup function
	
		private $baselocaldirectory;		//base local dir so files can be fully located by file_exists and gd library
		private $showdebugmessages;			//flag as to show the debug messages
		
		//-------------------------------
    	public function __construct() {
			$this->baselocaldirectory = $_SERVER['DOCUMENT_ROOT'];
			$this->showdebugmessages = false;
    	}
    	public function __destruct() {
 
    	}
		//-------------------------------
		public function getImg($imageloc, $width, $height, $alt, $class, $id) {
			$is_image_external	= false;		//used in case image isn't on the current server, which will change how the image is verified
			$is_file_image 		= false;		//flag show if images is valid
			$localimageloc 		= "";			//filename for local image location
			$returnimageloc		= "";			//image link to return
			
			//check imageloc... it might be a relative link or an absolute http/s, 
			//check and make local or an external link not on this server
			if (substr($imageloc, 0, 7) == "http://" || substr($imageloc, 0, 8) == "https://") {
				//remove server_name from the imageloc and tie onto baselocaldir
				$localimageloc = str_replace("https://". $_SERVER['SERVER_NAME'], "", $imageloc);
				$localimageloc = str_replace("http://". $_SERVER['SERVER_NAME'], "", $localimageloc);
				//check to see if http/s still exists on the off chance this function has been used with an external image link
				if (substr($localimageloc, 0, 7) == "http://" || substr($localimageloc, 0, 8) == "https://") { $is_image_external = true; }
				else { $localimageloc = $this->baselocaldirectory . $localimageloc; }
				//image is an absolute link so just copy
				$returnimageloc = $imageloc;
			}
			else {	//image is a relative link
				//check to see if link has a forward slash at the beginning, so our local link is correct (i.e. no "//")
				if (substr($imageloc, 0, 1) == "/") { $localimageloc = $this->baselocaldirectory . $imageloc; $returnimageloc = "http://" . $_SERVER['SERVER_NAME'] . $imageloc; }
				else { $localimageloc = $this->baselocaldirectory . "/" . $imageloc; $returnimageloc = "http://" . $_SERVER['SERVER_NAME'] . "/" . $imageloc; }
			}
			//--------------
			if ($this->showdebugmessages) echo "<p>localimageloc: $localimageloc</p>";
			if ($this->showdebugmessages) echo "<p>returnimageloc: $returnimageloc</p>";

			//does the image file exist? if so, then check to see if it is a valid image
			if (!$is_image_external) {
				if (is_file($localimageloc)) {
					$success = getimagesize($localimageloc);
					if ($success !== false) { //file is an image
						$is_file_image = true;
						if ($this->showdebugmessages) echo "<p>file is an image</p><br />";
					}
					else {	//file not an image
						$is_file_image = false;
						if ($this->showdebugmessages) echo "<p>file isn't an image</p><br />"; 
					}				
				}
				else { 	//file not found so we'll need to substitute
					$is_file_image = false;
					if ($this->showdebugmessages) echo "<p>file requested doesn't exist.</p>";
				}
			}
			else { //external image, different check conditions
				if ($this->checkRemoteFile($returnimageloc)) {
					if ($this->showdebugmessages) echo "'$returnimageloc' file located";
				}
				else {
					if ($this->showdebugmessages) echo "'$returnimageloc' image file doesn't exist";
					$is_file_image = false;
				}
			}
			//--------------
			// sort vars - needs reworking on overhaul of backupimage
			//--------------
			if ($width > 0) { $returnwidth = " width=\"". $width . "\""; }
			else { $returnwidth = ""; }
			if ($height > 0) $returnheight 	= " height=\"". $height . "\"";
			else { $returnheight = ""; }
			if ($alt != "") $returnalt = " alt=\"". $alt . "\"";
			else { $returnalt = ""; }
			if ($class != "") $returnclass = " class=\"". $class . "\"";
			else { $returnclass = ""; }
			if ($id != "") $returnid = " id=\"". $id . "\"";
			else { $returnid = ""; }
			//--------------
			
			//return the default replacement/backup image or supplied valid image
			if ($is_file_image) {
				return "<img src=\"". $returnimageloc ."\"" . $returnwidth . $returnheight . $returnalt . $returnclass . $returnid ." />";
			}
			else {
				return "<img src=\"". $this->singlebackupimage ."\"" . $returnwidth . $returnheight . $returnalt . $returnclass . $returnid ." />";
			}
		}
		//-------------------------------
		public function showImg($imageloc, $width, $height, $alt, $class, $id) {	//same as getImg but echos instead of returning a string
			echo $this->getImg($imageloc, $width, $height, $alt, $class, $id);
		}
		//-------------------------------
		public function enableDebugMessages() {	//turn on debugging and developing messages for this class
			$this->showdebugmessages = true;
		}
		public function disableDebugMessages() { //turn off debugging and developing messages for this class
			$this->showdebugmessages = false;
		}
		//-------------------------------
		public function setBackupImage($image) {
			if ($image != "") {
				$this->singlebackupimage = $image;
				return true;
			}
			else {
				return false;
			}
		}
		//-------------------------------
		public function getBackupImage() {
			return $this->singlebackupimage;
		}
		//-------------------------------
		public function getLocalBaseDir() {
			return $this->baselocaldirectory;
		}
		//-------------------------------
		//-------------------------------
		private function checkRemoteFile($url) { // taken from http://php.net/manual/en/function.getimagesize.php -> mycrazydream 24-Jul-2009 04:00
		    $ch = curl_init();

		    curl_setopt($ch, CURLOPT_URL, $url);
		    // don't download content
		    curl_setopt($ch, CURLOPT_NOBODY, 1);
		    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
		    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
		
		    if (curl_exec($ch) !== FALSE) {
		    	curl_close($ch);
		        return true;
		    }
		    else {
		    	curl_close($ch);
		        return false;
		    }
		}
		//-------------------------------
		
	}
?>